//
//  StoreDeatilVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2018/1/8.
//  Copyright © 2018年 LJL. All rights reserved.
//

#import "StoreDeatilVC.h"
#import "StoreCollectionViewCell.h"
#import "GoodsDetailVC.h"
#import "StoreHomeApi.h"
#import "StoreDetailApi.h"
@interface StoreDeatilVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)StoreDetailApi *api;
@property (nonatomic, copy)NSString *pageNumber;//页数
@property (nonatomic, copy)NSString *pageSize;//一页显示列表数
@property (nonatomic, copy)NSString *queryTime;//查询时间
@end

@implementation StoreDeatilVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = self.str;
    self.api = [StoreDetailApi new];
    _pageSize = @"10";
    _pageNumber = @"0";
    self.dataArr = [@[] mutableCopy];
    [self loadData];
    [self loadUI];
}

- (void)loadUI
{
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH,KSCREENHEIGHT) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    self.collectionView.backgroundColor = [UIColor c2Color];
    self.collectionView.alwaysBounceVertical = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"StoreCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"StoreCollectionViewCell"];
        @KWeakObj(self);
        [self.collectionView ig_setHeaderWithRefreshingBlock:^{
            _pageSize = @"10";
            _pageNumber = @"0";
            [selfWeak loadData];
        }];
    [self.view addSubview:self.collectionView];
}

- (void)loadData
{
    self.api.pageSize = _pageSize;
    self.api.pageNumber = _pageNumber;
    self.api.queryTime = _queryTime;
    self.api.type = _type;
    self.api.value = _value;
    @KWeakObj(self)
    [self.api startWithDecodeClass:[StoreDetailResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [self.collectionView ig_endHeaderRefreshing];
        [self.collectionView ig_endFooterRefreshing];
        if (responseObject)
        {
            StoreDetailResp *resp = responseObject;
            StoreDetailModel *model = resp.data;
            if (model.lstGoods && model.lstGoods.count > 0)
            {
                [selfWeak setupFooter];
            }
            else
            {
                [selfWeak.collectionView ig_endFooterRefreshingWithNoMoreData];
            }
            if ([_pageNumber isEqualToString:@"0"])
            {
                _dataArr = [NSMutableArray arrayWithArray:model.lstGoods];
            }
            else
            {
                [_dataArr addObjectsFromArray:model.lstGoods];
            }
            _queryTime = model.queryTime;
            [selfWeak.collectionView reloadData];
        }
        else
        {
            
        }
    }];
}

- (void)setupFooter
{
    @KWeakObj(self)
    [self.collectionView ig_setFooterWithRefreshingBlock:^{
        _pageNumber = [NSString stringWithFormat:@"%ld", [_pageNumber integerValue] + 1];
        [selfWeak loadData];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count > 0 ? _dataArr.count : 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (KSCREENWIDTH - 3) / 2;
    CGFloat height = width * 360 / 372;
    return CGSizeMake(width , height + 65);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsListModel *goodsModel = _dataArr[indexPath.item];
    StoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StoreCollectionViewCell" forIndexPath:indexPath];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:goodsModel.image]];
    cell.storeName.text = goodsModel.title;
    cell.priceL.text = goodsModel.price;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsListModel *goodsModel = _dataArr[indexPath.item];
    GoodsDetailVC *vc = [[GoodsDetailVC alloc] init];
    vc.goodsId = goodsModel.aId;
    [UIViewController ig_pushViewController:vc animated:YES];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(3, 0, 3, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 3;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 3;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



@end
